/*
 * Copyright 2006-2007 Queplix Corp.
 *
 * Licensed under the Modified Queplix BSD License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.queplix.com/BSDLICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jlect.swebing.ui.client.menu;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.jlect.swebing.ui.client.*;
import com.jlect.swebing.util.client.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * This component is the representation of the special menu layout,
 * it could be used
 *
 * @author Sergey Kozmin
 * @since Nov 26, 2007
 */
public class GMenuBar extends GContainer implements MouseListener, ActionListener {
    private static Logger logger = Logger.getLogger("GMenuBar");

    private MenuBarItemProvider provider;

    private Map menuItems = new HashMap();
    private MenuBarItem activeItem;
//    private boolean activated;
//    private SingleSelectionController selectionController = new SingleSelectionController();

    public GMenuBar() {
        this(new FlowLayout(), new MenuItemProvider());
    }

    public GMenuBar(LayoutManager layoutManager, MenuBarItemProvider provider) {
        super(layoutManager);
        this.provider = provider;
    }

    /**
     * Adds menu to the menu bar. It returns already added menu so client could
     * use transitive calls like bar.addMenu(aMenu).addMenu(anotherMenu)
     * @param menu menu to add
     * @return added menu.
     */
    public GMenu addMenu(GMenu menu) {
        MenuBarItem item = provider.createMenuBarItem(menu);
        GComponent view = item.getView();
        addComponent(view);
        view.addMouseListener(this);
        menu.addActionListener(this);
        menuItems.put(view, item);
        return menu;
    }
    
    //    todo create map item view to menu
    /*public void removeMenu(GMenu menu) {
        removeComponent(menu);
    }*/

    public void removeMenu(int index) {
        GComponent component = removeComponent(index);
        menuItems.remove(component);
        component.removeMouseListener(this);
    }

    public void mouseClicked(MouseEvent e) {
//        activated = true;
        MenuBarItem item = (MenuBarItem) menuItems.get(e.getSource());
        if(item != null) {
            item.activate();
            item.performAction();
            activeItem = item;
//        } else {
//            logger.warn("Mouse clicked on component, but selected component is null. event [" + e.toString() + "]. ");
        }
    }

    public void mousePressed(MouseEvent e) {
        //@todo implement me
    }

    public void mouseReleased(MouseEvent e) {
        //@todo implement me
    }

    public void mouseEntered(MouseEvent e) {
        MenuBarItem item = (MenuBarItem) menuItems.get(e.getSource());
        if(activeItem != null) {
            activeItem.deActivate();
            item.activate();
            activeItem = item;
            logger.info("Menu is shown. ");
            RootPanel.get().add(new Label("--------aa--------"));
        }
        /*if(item != null && activated) {
            if(selectedComp != null) {
                selectedComp.deActivate();
            }
            item.activate();
        }
        selectedComp = item;*/
    }

    public void mouseExited(MouseEvent e) {
//        MenuBarItem item = (MenuBarItem) menuItems.get(e.getSource());
//        item.deActivate();
    }

    public void actionPerformed(ActionEvent event) {
        activeItem.deActivate();
        activeItem = null;
    }
}
